'use strict';

/* Controllers */

var phonecatControllers = angular.module('phonecatControllers', []);

phonecatControllers.controller('PhoneListCtrl', ['$scope', 'Phone',
  function($scope, Phone) {
    $scope.phones = Phone.query();
    $scope.orderProp = 'age';
    $scope.add = false;
    $scope.mask = 'mask';
    $scope.submitButton = 'undo';

    $scope.Add = function(){
      $scope.add = !$scope.add;
      $scope.submitButton = 'OK';
      $scope.editId=null;
      $scope.formData={}; 
      if($scope.mask == "mask open"){
        $scope.mask = "mask";
      }else{
        $scope.mask = "mask open";
      }
	  $scope.phones = Phone.query();
      
    };
    $scope.Action = function(submitButton){
      var submitButton = submitButton;
      var eId = $scope.editId;
      console.log(eId);
      if(submitButton == 'OK' && eId == null){
        $scope.Save();
      }else if(submitButton == 'Update' && eId!=null){
        console.log(eId);
        $scope.UID= eId;
        $scope.Update(eId);
      }else{
        $scope.Add();
      }    
    };
	
	$scope.Edit = function(id){
      $scope.editId = id;
      var editId = id;
      $scope.formData = Phone.get({id:id});
      $scope.add = !$scope.add;
      $scope.submitButton = 'Update';
      if($scope.mask == "mask open"){
        $scope.mask = "mask";
      }else{
        $scope.mask = "mask open";
      }  
    };
    $scope.Save = function(){
	  console.log('I am save();');
      Phone.save({},$scope.formData);
      $scope.Add();
      $scope.phones = Phone.query();
    };
	
    $scope.Delete = function(id){
	    var deleteId = id;
      Phone.delete({id:deleteId});
      $scope.phones = Phone.query();
    };
	
    $scope.Update = function(id){
      console.log('I am update();');
      var updateId = id;
      $scope.UID= id;
      Phone.save({id:updateId},$scope.formData);
      $scope.Add();   
    };


  }]);

phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', 'Phone',
    function($scope, $routeParams, Phone) {
    $scope.phone = Phone.get({id:$routeParams.phoneId});
  // function($scope, $routeParams, Phone) {
  //   $scope.phone = Phone.get({phone: $routeParams.phoneId}, function(phone) {
  //     $scope.mainImageUrl = phone.images[0];
  //   });

    // $scope.setImage = function(imageUrl) {
    //   $scope.mainImageUrl = imageUrl;
    // };
  }]);
